package com.gruopbuying.agent.service.impl;

import com.gruopbuying.agent.connector.CheetahFeign;
import com.gruopbuying.agent.connector.StoreFeign;
import com.gruopbuying.agent.constants.SysAgentInfoEnumConstants;
import com.gruopbuying.agent.context.FilterContextHandler;
import com.gruopbuying.agent.dao.SysAgentInfoDao;
import com.gruopbuying.agent.domain.SysAgentInfoDO;
import com.gruopbuying.agent.service.SysAgentInfoService;
import com.gruopbuying.agent.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SysAgentInfoServiceImpl implements SysAgentInfoService {

	private static final Logger logger = LoggerFactory.getLogger(SysAgentInfoServiceImpl.class);

	@Autowired
	private SysAgentInfoDao sysAgentInfoDao;

	@Autowired
	private CheetahFeign cheetahFeign;
    @Autowired
	private StoreFeign storeFeign;

	@Override
	public SysAgentInfoDO get(String id){
		return sysAgentInfoDao.get(id);
	}

	@Override
	public PageUtils agentFlowList(Map<String, Object> map) {
		PageUtils page=this.cheetahFeign.getagentMoneyFlows(map);
		return page;
	}
	@Override
	public List<SysAgentInfoDO> list(Map<String, Object> map){
		map.put("isDelete",SysAgentInfoEnumConstants.ENUM_ISDELETE_UNDEL);
        map.put("agentRel",map.get("agentId"));
		map.put("order", "desc");
		List<SysAgentInfoDO> sysAgentInfoList = sysAgentInfoDao.list(map);
		for (SysAgentInfoDO sysAgentInfo : sysAgentInfoList) {
			if(sysAgentInfo.getStatus().equals("00")){
				sysAgentInfo.setStatusName("停用");
			}else if(sysAgentInfo.getStatus().equals("01")){
				sysAgentInfo.setStatusName("正常");
			}else{
				sysAgentInfo.setStatusName("冻结");
			}
			String address = sysAgentInfo.getProvince() + " "+sysAgentInfo.getCity();
			sysAgentInfo.setAddress(address);
		}
		return sysAgentInfoList;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return sysAgentInfoDao.count(map);
	}

	/**
	 * 通过代理商id获取服务商列表
	 */

	@Override
	public PageUtils getStoreAccountList(Map<String, Object> map) {
		/*从token获取代理商id*/
		String agentId = FilterContextHandler.getAgentId();
		logger.info("SysAgentInfoController.getStoreAccountList()----->获取到的代理商id为:"+agentId);
		if(agentId.equals(SysAgentInfoEnumConstants.ENUM_SUPER_AGENT)){
			map.put("providerType","0");
		}else{
			map.put("agentId", agentId);
			map.put("providerType", "0");
		}
		PageUtils sysAgentInfoList=storeFeign.getStoreAccountList(map);
		return sysAgentInfoList;
	}
	/**
	 * 通过代理商id获取服务门店商列表
	 */
	@Override
	public PageUtils storeMainInfoListByAgent(Map<String, Object> map) {
		/*从token获取代理商id*/
		String agentId = FilterContextHandler.getAgentId();
		logger.info("SysAgentInfoController.storeMainInfoListByAgent()----->获取到的代理商id为:"+agentId);
		map.put("id", agentId);
		map.put("providerType", "0");
		PageUtils storeMainInfoListByAgent=storeFeign.storeMainInfoListByAgent(map);
		return storeMainInfoListByAgent;
	}
	/**
	 * 通过代理商id获取门店运营人员列表
	 */
	@Override
	public PageUtils getStoreOperList(Map<String, Object> map) {
		/*从token获取代理商id*/
		String agentId = FilterContextHandler.getAgentId();
		logger.info("SysAgentInfoController.getStoreOperList()----->获取到的代理商id为:"+agentId);
		map.put("id", agentId);
		map.put("providerType", "0");
		PageUtils getStoreOperList=storeFeign.getStoreOperList(map);
		return getStoreOperList;
	}

	/**
	 * 登录界面
	 */
	@Override
	public SysAgentInfoDO getByAgentNo(String agentNo) {
		return sysAgentInfoDao.getByAgentNo(agentNo);
	}


}
